<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<!-- <el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="120px">
					<el-row>
						<el-col :span="8">
							<el-form-item label="名称" prop="name">
								<el-input v-model="tableData.param.name" placeholder="请输入名称"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="状态" prop="status">
								<el-select v-model="tableData.param.status" placeholder="请选择状态" clearable >
									<el-option v-for="item in dictionary.statusList3" :label="item.label"
										:value="item.value" :key="item.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form> -->
				
				<el-button v-if="permissionStore.isPermissionsBtn('001009001')" size="default" type="success"
							class="ml10" @click="editIndustries(null)">
							<el-icon>
								<FolderAdd />
							</el-icon>
							新增行业
						</el-button>
			</div>
			<el-table v-loading="loading" :data="tableData.data" row-key="uniqueKey" :tree-props="{ children: 'scene' }">
				<el-table-column label="行业" prop="name" min-width="100px">
					<template #default="{ row }">{{ !row.industryId ? row.name : '' }}</template>
				</el-table-column>
				<el-table-column label="场景" prop="name" min-width="100px">
					<template #default="{ row }">{{ row.industryId ? row.name : '' }}</template>
				</el-table-column>
				<el-table-column label="前缀" prop="prefix" min-width="100px"></el-table-column>
				<el-table-column label="状态" align="center" min-width="100px">
					<template #default="{ row }">
						<el-tag :type=" row.status=='ENABLED'?'success':'danger'">{{ row.status=='ENABLED'?'启用':'停用' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" v-if="permissionStore.isPermissionsBtn(['001009002','001009003'])"
					class-name="small-padding" min-width="200px" fixed="right">
					<template #default="scope">
						<el-button size="small" v-if="permissionStore.isPermissionsBtn('001009002') && !scope.row.industryId" text type="primary"
							@click="editScene({industryId:scope.row.id})"><el-icon>
								<EditPen />
							</el-icon>新增场景</el-button>
						<el-button size="small" v-if="permissionStore.isPermissionsBtn('001009003')" text type="primary"
							@click="handleUpdate(scope.row)"><el-icon>
								<EditPen />
							</el-icon>修改</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<editScene ref="editSceneRef" @getList="getList"></editScene>
		<editIndustriesView ref="editIndustriesRef" @getList="getList"></editIndustriesView>
	</div>
</template>
<script lang="ts">
import { industriesListApi } from '@/api/auth/industries';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission"
import dictionary from '@/utils/dictionary.json'
import editScene from './component/editIndustriesScene.vue';
import editIndustriesView from './component/editIndustries.vue';
export default defineComponent({
	name: 'enterpriseList', //企业列表
	components: {
		editScene,
		editIndustriesView
	},
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const editSceneRef = ref();
		const permissionStore = usePermissionStore()
	
		const state = reactive({
			tableData: {
				data: [],
				loading: false,
				param: {
					status:undefined,
					name:undefined,
				},
			},
		});

		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮登录 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			industriesListApi(state.tableData.param).then((res: any) => {
				let list = res.data ?? [];
				list.forEach(item => {
					item.uniqueKey = `${item.id}`;
					if (item.scene) {
						item.scene.forEach(child => {
							child.uniqueKey = `${child.industryId}-${child.id}`;
						});
					}
				});

				state.tableData.data = list
				loading.value = false;
			});
		};
		
		// 新增,修改行业
		const editIndustriesRef=ref()
		const editIndustries=(row)=>{
			editIndustriesRef.value.openDialog(row)
		}

		// 新增,修改场景
		const editScene=(row)=>{
			editSceneRef.value.openDialog(row)
		}

		const handleUpdate = (row) => {
			if(row.industryId){
				let obj={
					id:row.id,
					name:row.name,
					status:row.status,
					industryId:row.industryId,
					prefix:row.prefix
				}
				editScene(obj)
			}else{
				let obj={
					id:row.id,
					name:row.name,
					status:row.status,
					prefix:row.prefix
				}
				editIndustries(obj)
			}

		};

		
		return {
			proxy,
			editSceneRef,
			loading,
			queryRef,
			permissionStore,
			resetQuery,
			getList,
			handleUpdate,
			editScene,
			dictionary,
			editIndustries,
			editIndustriesRef,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
